package com.cristina.attention.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.cristina.attention.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * @Author superman
 * @Date 2021/11/25 22:48
 **/
@Component
public class IBatisColumnsHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        String currentUsername = SecurityUtils.getCurrentUsername();
        this.strictInsertFill(metaObject, "createBy", String.class, currentUsername);
        this.strictInsertFill(metaObject, "updateBy", String.class, currentUsername);
        this.strictInsertFill(metaObject, "createTime", Long.class, System.currentTimeMillis());
        this.strictInsertFill(metaObject, "updateTime", Long.class, System.currentTimeMillis());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String currentUsername = SecurityUtils.getCurrentUsername();
        this.strictUpdateFill(metaObject, "updateTime", Long.class, System.currentTimeMillis());
        this.strictUpdateFill(metaObject, "updateBy", String.class, currentUsername);
    }
}
